Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Use wc-react
to simplify usage of web components in React. This is a is a light wrapper to helps manage events and props of the web component.
const MyComponent = wrapWc('my-web-component');
The wrapper syncs props and events to the web component, including when they change and when the component is unmounted.
npm install wc-react
or
yarn add wc-react
Import wrapWc
at the top:
import {wrapWc} from 'wc-react';
Create a new component that wraps your web component by calling the wrapWc
function and pass the tag name of the web component.
const MyComponent = wrapWc('my-web-component');
You can now use MyComponent
anywhere in your JSX as if it were a regular React component.
For example, you can set the someProp
property of the web component to an object:
const App = (props) => {
const someObj = {
prop1: 'bla'
}
return <MyComponent someProp={someObj}></MyComponent>;
}
Or register event handlers:
const App = (props) => {
const handleEvent = (e) => {}
return <MyComponent event={handleEvent}></MyComponent>;
}
All properties and events map exactly as they are defined on the web component.
wrapWc
supports optional props type to ensure type safety when using the component:
type PersonProps = {
personDetails: PersonDetails, // object
showName: boolean,
personCardInteraction: PersonCardInteraction // enum
}
const Person = wrapWc<PersonProps>('mgt-person');
By default, if no type is provided, any prop will be valid.
If you've used web components in React, you know that proper interop between web components and React components requires a bit of extra work.
From https://custom-elements-everywhere.com/:
React passes all data to Custom Elements in the form of HTML attributes. For primitive data this is fine, but the system breaks down when passing rich data, like objects or arrays. In these instances you end up with stringified values like some-attr="[object Object]" which can't actually be used.
Because React implements its own synthetic event system, it cannot listen for DOM events coming from Custom Elements without the use of a workaround. Developers will need to reference their Custom Elements using a ref and manually attach event listeners with addEventListener. This makes working with Custom Elements cumbersome.
Here is an example of using the vaading-date-picker
web component in React.
wc-react
:import React from 'react';
import '@vaadin/vaadin-date-picker';
const App: React.FunctionComponent = () => {
const handleChange = (e) => {/**/}
const localizedStrings = {/**/}
return <vaadin-date-picker
label="When were you born?"
ref={(element) => {
if (!element) {
return;
}
element.i18n = localizedStrings;
// the event listener needs to be removed
// when the element is unloaded - not shown here
element.addEventListener('change', (e) => handleChange(e));
}}>
</vaadin-date-picker>;
};
wc-react
:import React from 'react';
import '@vaadin/vaadin-date-picker';
import { wrapWc } from 'wc-react';
const DatePicker = wrapWc('vaading-date-picker');
const App: React.FunctionComponent = () => {
const handleChange = (e) => {/**/}
const localizedStrings = {/**/}
return <DatePicker
label="When were you born?"
change={handleChange}
i18n={localizedStrings}>
</DatePicker>;
};
FAQs
Web Components wrapper class for React
The npm package wc-react receives a total of 4,773 weekly downloads. As such, wc-react popularity was classified as popular.
We found that wc-react demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.